package team.pra.manager.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.pra.manager.common.ManagerResult;
import team.pra.manager.common.PageHelper;
import team.pra.manager.pojo.RoomType;
import team.pra.manager.service.RoomTypeService;

import javax.servlet.http.HttpServletRequest;

/**
 * 房型管理的CURD
 */
@RestController
@RequestMapping("/roomtype")
public class RoomTypeController {
    @Autowired
    private RoomTypeService roomTypeService;

    @GetMapping("/list")
    @RequiresPermissions({"select"})
    public PageHelper listRoomType(@RequestParam String token, PageHelper<RoomType> pageHelper,
                                   @RequestParam(name = "search[value]", defaultValue = "") String searchvalue,
                                   @RequestParam(name = "order[0][column]", defaultValue = "") Integer orderColunm,
                                   @RequestParam(name = "order[0][dir]", defaultValue = "") String orderDir,
                                   HttpServletRequest request) {
        String columnName = request.getParameter("columns["+orderColunm+"][data]");
        pageHelper.setColumnName(columnName);
        pageHelper.setSearchValue(searchvalue);
        pageHelper.setOrderDir(orderDir);
        roomTypeService.findRoomTypeByCondition(pageHelper);
        return pageHelper;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @RequiresPermissions({"insert"})
    public ManagerResult insertRoomType(@RequestParam String token,RoomType roomType) {
        return roomTypeService.insertRoomType(roomType);
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @RequiresPermissions({"update"})
    public ManagerResult updateRoomType(RoomType roomType) {
        return roomTypeService.updateRoomType(roomType);
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @RequiresPermissions({"delete"})
    public ManagerResult deleteRoomType(String token, Integer id) {
        return roomTypeService.deleteRoomType(id);
    }
}
